package com.feihao.demo01DateJDK8;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Demo01LocalDate {
    public static void main(String[] args) throws ParseException {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);


        LocalDateTime localDateTime1 = LocalDateTime.of(1998,06,18,6,15);
        System.out.println(localDateTime1);


        //获取当前时间对象的年份。
        int year = localDateTime.getYear();
        System.out.println(year);

        //枚举类型月分
        Month month = localDateTime.getMonth();
        System.out.println(month);

        //获取当前时间对象的月份。
        int monthValue = localDateTime.getMonthValue();
        System.out.println(monthValue);


        //获取当前时间对象的月份中的哪一天
        int dayOfMonth = localDateTime.getDayOfMonth();
        System.out.println(dayOfMonth);

        //获取的是当前周的枚举类型（）
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        System.out.println(dayOfWeek);


        int second = localDateTime.getSecond();
        System.out.println(second);


        //LocalDateTime转换为LocalData:
        LocalDate localDate = localDateTime.toLocalDate();
        System.out.println(localDate);


        LocalTime localTime = localDateTime.toLocalTime();
        System.out.println(localTime);



        //格式化：
        //首先创建DataTimeFormat对象，指定格式化形式："yyyy年MM月dd日 HH:mm:ss"
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");


        String format = localDateTime.format(dateTimeFormatter);
        System.out.println("JDK8:::"+format);



        //JDK7之前的
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format1 = simpleDateFormat.format(date);
        System.out.println("JDK7:::::"+format1);



        //解析：

        String data = "2021年03月24日 08:50:17";

        //JDK7之前：
        Date parse = simpleDateFormat.parse(data);
        System.out.println(parse);

        //JDK8:
        LocalDateTime parse1 = LocalDateTime.parse(data,dateTimeFormatter);
        System.out.println(parse1);

        LocalDateTime localDateTime2 = localDateTime.plusYears(1);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);



    }
}
